package com.mrjy.maintain.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserBindEmail;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.integral.dao.IIntegralInfoDao;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.maintain.dao.IUserBindEmailDao;
import com.mrjy.maintain.dao.IUserInfoDao;

@Service("userBindEmailBo")
public class UserBindEmailBo implements IUserBindEmailBo {

	@Resource
	private IUserBindEmailDao userBindEmailDao;
	@Resource
	private IUserInfoDao userInfoDao;
	@Resource
	private IIntegralInfoDao integralInfoDao;
	
	@Override
	public Map<String , Object> txBindEmailExpire(String encode) {
		Map<String , Object> map = new HashMap<String, Object>();
		UserBindEmail userBindEmail = userBindEmailDao.queryByMark(encode);
		if (userBindEmail == null) {
			map.put("err_code", "error");
			return map;
		}else {
			if (userBindEmail.getDisableFlag().intValue()==0) {
				String date = DateUtil.longToString(new  Date().getTime(), DateUtil.FULL_DATE);
				if (date.compareTo(userBindEmail.getExpire()) <= 0) {
					if (userInfoDao.queryByEmail(userBindEmail.getBindingEmail()) != null) {
						map.put("err_code", "emailBinded");
						return map;
					}
					UserInfo userInfo = userInfoDao.queryById(userBindEmail.getUserId());
					userInfo.setEmail(userBindEmail.getBindingEmail());
					
					int row = userInfoDao.updateById(userInfo);
					if (row != 1) {
						map.put("err_code", "error");
						return map;
					}
					//新增需求，绑定邮箱得积分
					IntegralInfo paramInfo = new IntegralInfo();
		            paramInfo.setUserId(userInfo.getId());
		            paramInfo.setInfoId(userInfo.getId());
	            	//根据用户id和类型id查询其有没有记录
	            	paramInfo.setTypeId(IntegralEnum.BIND_EMAIL.getTypeId());
	        		List<IntegralInfo> list = integralInfoDao.queryListByInfo(paramInfo);
	        		if(list == null || list.size() == 0){
	        			IntegralInfo integral = IntegralUtil.setIntegral(userInfo.getId()
	            				, IntegralEnum.BIND_EMAIL.getScore()
	            				, IntegralEnum.BIND_EMAIL.getTypeDesc()
	            				, IntegralEnum.BIND_EMAIL.getTypeId()
	            				, userInfo.getId());
	        		}
					
					
					userBindEmail.setDisableFlag(1);
					userBindEmailDao.updateById(userBindEmail);
					map.put("err_code", "ok");
					map.put("userBindEmail", userBindEmail.getBindingEmail());
					return map;
				} else {
					map.put("err_code", "expire");
					return map;
				}
			}else {
				map.put("err_code", "expire");
				return map;
			}
			
		}
	}

	@Override
	public Object checkBindEmailState(UserInfo userInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		UserBindEmail userBindEmail = userBindEmailDao.queryRecentByUserId(userInfo.getId());
		if (userBindEmail==null) {
			return "error";
		}else {
			String date = DateUtil.longToString(new  Date().getTime(), DateUtil.FULL_DATE);
			if (date.compareTo(userBindEmail.getExpire())<=0) {
				UserBindEmail ube = new UserBindEmail();
				ube.setBindingEmail(userBindEmail.getBindingEmail());
				ube.setText("pending_verification");
				return ube;
			} else {
				return "error";
			}
		}
	}

}
